import Vue from 'vue'
import VueRouter from 'vue-router'
import ROUTER_MAP from './router-map'
import {
  Modal
} from 'ant-design-vue'
import store from '@/store'
import mTypes from "@/store/mutationsTypes";
import {
  getQueryString
} from '@/utils/tools'
Vue.use(VueRouter)
const originalPush = VueRouter.prototype.push
VueRouter.prototype.push = function push (location) {
  return originalPush.call(this, location).catch(err => err)
}
const originalPush1 = VueRouter.prototype.replace
VueRouter.prototype.replace = function push (location) {
  return originalPush1.call(this, location).catch(err => err)
}
const routes = [{
  path: '/',
  name: 'Index',
  component: () => import( /* webpackChunkName: "Index" */ '../components/layout/Layout.vue'),
  children: [{
    path: '/',
    name: ROUTER_MAP.home,
    component: () => import( /* webpackChunkName: "Home" */ '../views/home/index.vue'),
  }, {
    path: '/course-center',
    meta: {
      auth: false,
    },
    name: ROUTER_MAP.courseCenter,
    component: () => import( /* webpackChunkName: "CourseCenter" */ '../views/course-center/index.vue'),
  },  {
    path: '/courseDetail',
    meta: {
      auth: false,
    },
    name: ROUTER_MAP.courseDetail,
    component: () => import( /* webpackChunkName: "CourseCenter" */ '../views/course-center/courseDetail.vue'),
  }, {
    path: '/person-center',
    meta: {
      auth: true,
    },
    name: ROUTER_MAP.personCenter,
    component: () => import( /* webpackChunkName: "CourseCenter" */ '../views/person-center/index.vue'),
    children: [
      {
        path: '/',
        name: 'person',
        component: () => import ('../views/person-center/person.vue')
      },
      {
        path: 'personInfo',
        name: 'personInfo',
        component: () => import ('../views/person-center/person.vue')
      },
      {
        path: 'course',
        name: 'course',
        component: () => import ('../views/person-center/course.vue')
      },
      {
        path: 'courselist',
        name: 'courselist',
        component: () => import ('../views/person-center/courselist.vue')
      },
      {
        path: 'study',
        name: 'study',
        component: () => import ('../views/person-center/study.vue')
      },
      {
        path: 'showConnect',
        name: 'showConnect',
        component: () => import ('../views/person-center/showConnect.vue')
      },
      {
        path: 'myWork',
        name: 'myWork',
        component: () => import ('../views/person-center/myWork.vue')
      },
      {
        path: 'myOrder',
        name: 'myOrder',
        component: () => import ('../views/person-center/myOrder.vue')
      },
      {
        path: 'myMessage',
        name: 'myMessage',
        component: () => import ('../views/person-center/myMessage.vue')
      }
    ]
  }, {
    path: '/mycourse',
    name: ROUTER_MAP.myCourse,
    meta: {
      auth: true,
    },
    component: () => import( /* webpackChunkName: "myCourse" */ '../views/mycourse/index.vue'),
  }, {
    path: '/mydoublecourse',
    name: ROUTER_MAP.myDoubleCourse,
    meta: {
      auth: true,
    },
    component: () => import( /* webpackChunkName: "myCourse" */ '../views/mycourse/doubleminusindex.vue'),
  }, {
    path: '/register',
    name: ROUTER_MAP.register,
    component: () => import( /* webpackChunkName: "register" */ '../views/register/index.vue'),
  }, {
    path: '/teacher-login',
    name: ROUTER_MAP.teacherLogin,
    component: () => import( /* webpackChunkName: "teacherLogin" */ '../views/teacher-login/index.vue')
  }, {
    path: '/exam',
    name: ROUTER_MAP.goExam,
    meta: {
      auth: true,
    },
    component: () => import('../views/exam/index.vue'),
  }, {
    path: '/wordexam',
    name: 'startExam',
    component: () => import('../views/exam/wordexam.vue'),
  }, {
    path: '/examReport',
    name: 'examReport',
    component: () => import('../views/exam/examReport.vue'),
  }]
},
  {
    path: '/room',
    name: 'Room',
    meta: {
      auth: true,
    },
    component: () => import( /* webpackChunkName: "Room" */ '../components/Room/index.vue')
  },
  {
    path: '/yxproom',
    name: 'YxpRoom',
    meta: {
      auth: true,
    },
    component: () => import( /* webpackChunkName: "Room" */ '../views/room/index.vue')
  },
  {
    path: '/registration',
    name: 'registration',
    component: () => import('../views/agreement/registerAgreement.vue')
  },
  {
    path: '/privacyprotection',
    name: 'privacyprotection',
    component: () => import('../views/agreement/privacyAgreement.vue')
  },
  {
    path: '/playCourse',
    name: 'playCourse',
    component: () => import('../views/course-center/playCourse.vue')
  }
]

const router = new VueRouter({
  mode: 'hash',
  base: process.env.BASE_URL,
  routes,
  scrollBehavior(to, from, savedPosition) {
    return {
      x: 0,
      y: 0
    }
  }
})

router.beforeEach((to, from, next) => {
  console.log(to)
  // 关闭 modal
  Modal.destroyAll()
  console.log(to.meta.auth, store.state)
  if (to.meta.auth) {
    if (store.state.access_token || getQueryString('access_token')) {
      next()
    } else {
      store.commit(mTypes.loginModal, true)
    }
  } else {
    store.commit(mTypes.loginModal, false)
    next()
  }
})

export default router
